<layout name="page" />
<block name="content">
	<input type="hidden" name="ajax" id="ajax" value="1">
	{:widget('page_header/search',array('name'=>'权限分配','search'=>'S'))}
	<div class="operate-panel">
		<i class="mid"></i>
		<a class="btn btn-normal" onclick="save()">保存</a>
	</div>
	<form id="form_data" name="form_data" method="post">
		<div class="x1 box sub">
			<div class="x1 w30 m100">
				<div class="table" id="user_list">
					<ul>
						<li class="thead" >
							<span class="w3" ><label class="checkbox">
									<input type="checkbox" class="toggle-select-all" data="user_id[]"  />
									<i class="icon"></i></label></span>
							<span class="w10" >员工编号</span>
							<span class="w10" >姓名</span>
							<span class="w10">部门</span>
							<span class="w10">职位</span>
							<span class="x1"></span>
							<span class="w10">状态</span>
						</li>
						<foreach name="user_list" val="data">
							<li class="tbody" data="{$data.id}">
								<span class="w3" ><label class="checkbox" >
										<input type="checkbox" name="user_id[]" value="{$data.id}" />
										<i class="icon"></i></label></span>
								<span class="w10">{$data.emp_no}</span>
								<span class="w10 ">{$data.name}</span>
								<span class="w10 ">{$data.dept_name}</span>
								<span class="w10 ">{$data.position_name}</span>
								<span class="x1"></span>
								<span class="w10">{:status($data['is_del'])}</span>
							</li>
						</foreach>
					</ul>
				</div>
			</div>
			<div style="width:16px;height:32px;"></div>
			<div class="w30 m100" style="height:720px;">
				<div class="table" id="role_list">
					<ul>
						<li class="thead" >
							<span class="x1" >权限组</span>
						</li>
						<foreach name="role_list" val="data">
							<li class="tbody" data="{$data.id}">
								<span class="w3" ><label class="checkbox" >
										<input type="checkbox" id="role_{$data.id}" name="role_list[]" value="{$data.id}" />
										<i class="icon"></i></label></span>
								<span class="w10 x1 ">{$data.name}</span>
							</li>
						</foreach>
					</ul>
				</div>
			</div>
		</div>
	</form>
</block>
<block name="js">
	<script type="text/javascript">
		layui.use(['global'], function() {
			set_return_url(null);

			var $ = layui.jquery;
			var layer = layui.layer;

			var fn = {
				save : function() {
					if ($("input:checked[name='user_id[]']").length < 1) {
						layer.msg("请选择用户");
						return false;
					};
					if ($("input:checked[name='role_list[]']").length < 1) {
						layer.msg("请选择权限");
						return false;
					};
					send_form('form_data', '{:url("set_role")}', '', function(ret) {
						if (ret.status == 1) {
							layer.msg(ret.info);
							//parent.location.reload(true);
						} else {
							layer.msg(ret.info);
							return false;
						}
					});
				},
			};
			win_exp(fn);

			$("#user_list li.tbody").click(function() {
				$("#user_list li.tbody").removeClass('active');
				$this = $(this);
				$this.addClass("active");

				if ($("input:checked", $this).length == 0) {
					$("input", $this).prop('checked', true);
				} else {
					$("input", $this).prop('checked', false);
				}

				var vars = {};
				vars.id = $this.attr("data");
				send_ajax("{:url('get_role_list')}", vars, function(data) {
					show_data(data);
				});
				return false;
			});

			$('#role_list li.tbody').click(function() {
				$this = $(this);
				if ($("input:checked", $this).length == 0) {
					$("input", $this).prop('checked', true);
				} else {
					$("input", $this).prop('checked', false);
				}
			});

			$('#user_list input,#role_list input').click(function() {
				event.stopPropagation();
			});

			function show_data(result) {
				$("input[name='role_list[]']").each(function() {
					$(this).prop('checked', false);
				});
				for (var s in result.data) {
					$("#role_" + result.data[s].role_id).prop('checked', true);
				}
			};
		});
	</script>
</block>